import { useMemo } from 'react';
import usePlanStore from '../store/planStore';
import planService from '../services/planService';
import { getToday, getWeekRange, getCurrentMonth } from '../utils/dateUtils';

/**
 * 统计数据相关的自定义 Hook
 */
export const useStatistics = () => {
  const { plans, weeklyPlans, monthlyPlans, settings } = usePlanStore();

  // 今日统计
  const todayStats = useMemo(() => {
    const today = getToday();
    const todayPlans = plans.filter((p) => p.scheduledFor === today);
    return {
      total: todayPlans.length,
      completed: todayPlans.filter((p) => p.status === 'completed').length,
      completionRate: planService.calculateCompletionRate(todayPlans),
    };
  }, [plans]);

  // 本周统计（包含日计划和周计划及其子任务）
  const weekStats = useMemo(() => {
    const weekRange = getWeekRange(new Date(), settings.weekStartDay);
    
    // 本周的日计划
    const weekDailyPlans = plans.filter(
      (p) => p.scheduledFor >= weekRange.weekStart && p.scheduledFor <= weekRange.weekEnd
    );
    
    // 本周的周计划
    const currentWeekPlans = weeklyPlans.filter(
      (p) => p.weekStart === weekRange.weekStart
    );
    
    // 统计日计划
    const dailyTotal = weekDailyPlans.length;
    const dailyCompleted = weekDailyPlans.filter((p) => p.status === 'completed').length;
    
    // 统计周计划及其子任务
    let weeklyTotal = 0;
    let weeklyCompleted = 0;
    
    currentWeekPlans.forEach((weeklyPlan) => {
      const tasks = weeklyPlan.tasks || [];
      weeklyTotal += tasks.length;
      weeklyCompleted += tasks.filter((t) => t.status === 'completed').length;
    });
    
    // 合并统计
    const total = dailyTotal + weeklyTotal;
    const completed = dailyCompleted + weeklyCompleted;
    const completionRate = total > 0 ? Math.round((completed / total) * 100) : 0;
    
    return {
      total,
      completed,
      completionRate,
    };
  }, [plans, weeklyPlans, settings.weekStartDay]);

  // 本月统计（包含日计划和月计划及其子任务）
  const monthStats = useMemo(() => {
    const currentMonth = getCurrentMonth();
    
    // 本月的日计划
    const monthDailyPlans = plans.filter((p) => p.scheduledFor.startsWith(currentMonth));
    
    // 本月的月计划
    const currentMonthPlans = monthlyPlans.filter((p) => p.month === currentMonth);
    
    // 统计日计划
    const dailyTotal = monthDailyPlans.length;
    const dailyCompleted = monthDailyPlans.filter((p) => p.status === 'completed').length;
    
    // 统计月计划及其子任务
    let monthlyTotal = 0;
    let monthlyCompleted = 0;
    
    currentMonthPlans.forEach((monthlyPlan) => {
      const tasks = monthlyPlan.tasks || [];
      monthlyTotal += tasks.length;
      monthlyCompleted += tasks.filter((t) => t.status === 'completed').length;
    });
    
    // 合并统计
    const total = dailyTotal + monthlyTotal;
    const completed = dailyCompleted + monthlyCompleted;
    const completionRate = total > 0 ? Math.round((completed / total) * 100) : 0;
    
    return {
      total,
      completed,
      completionRate,
    };
  }, [plans, monthlyPlans]);

  // 总体统计
  const overallStats = useMemo(() => {
    return {
      total: plans.length,
      completed: plans.filter((p) => p.status === 'completed').length,
      completionRate: planService.calculateCompletionRate(plans),
    };
  }, [plans]);

  return {
    todayStats,
    weekStats,
    monthStats,
    overallStats,
  };
};

